﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework;

namespace uam_fps_game.Utils
{
    public class FpsCounter
    {
        SpriteBatch mSpriteBatch;
        SpriteFont mFont;

        int mFrames = 0;
        double mElapsedTimeSec = 0.0;

        int mFps = 0;
        float mFrameTime = 0.0f;

        public FpsCounter(GraphicsDevice device, ContentManager content)
        {
            mSpriteBatch = new SpriteBatch(device);
            mFont = content.Load<SpriteFont>("Fonts\\SpriteFont1");
        }

        public void Update(GameTime gameTime)
        {
            mElapsedTimeSec += gameTime.ElapsedGameTime.TotalSeconds;

            if (mElapsedTimeSec > 1.0)
            {
                mFps = mFrames;
                mFrameTime = 1.0f / (float)mFrames;

                mFrames = 0;
                mElapsedTimeSec = 0.0;
            }
            else
            {
                mFrames++;
            }
        }

        public void Draw()
        {
            mSpriteBatch.Begin();
            mSpriteBatch.DrawString(mFont, "FPS: " + mFps.ToString(), new Vector2(0.0f, 0.0f), Color.White);
            mSpriteBatch.DrawString(mFont, "Frame time: " + mFrameTime.ToString(), new Vector2(0.0f, 20.0f), Color.White);
            mSpriteBatch.End();
        }
    }
}
